/******************************************************************************
 QuoteScraperService extracts stock quotes from finance.google.com
    Copyright (C) 2011  Benjamin A. Gura

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/

#include <iosfwd>

#include "DBAccess.hpp"

namespace bagprod { namespace finance { namespace SPS {
    
DBAccess::DBAccess()
{

}
void DBAccess::Initialize(std::string database, std::string priceTable,
        std::string stockTable, std::string host, std::string user,
        std::string pass, int port)
{
    this->m_priceTable = priceTable;
    this->m_stockTable = stockTable;

    if( !m_connection.connect(database.c_str(),host.c_str(),user.c_str(),
            pass.c_str(),port) )
    {
        std::cerr << "Could not establish database connection\n";
        exit(1);
    }
}
std::list<types::Stock*>& DBAccess::GetStocks()
{
    std::list<types::Stock*> stockList;
    
    mysqlpp::Query query = m_connection.query("SELECT * FROM %0:field1;");
    query.parse();
    
    // Execute query
    if( mysqlpp::StoreQueryResult result = query.store(m_stockTable))
    {
        for( size_t i = 0; i < result.num_rows(); i++)
        {
            stockList.push_back(new types::Stock(
                                (std::string)result[i]["ticker"],
                                (std::string)result[i]["company"],
                                (long)result[i]["sid"]));
        }
    }
    
    return(stockList);
}
std::list<types::StockData*>& DBAccess::GetStockData(long stockId)
{
    std::list<types::StockData*> stockDataList;
    
    mysqlpp::Query query = m_connection.query("SELECT * FROM %1:price WHERE"
            " sid = %0:id ORDER BY Date ASC;" );
    query.parse();
    
    if ( mysqlpp::StoreQueryResult result = query.store(m_priceTable,stockId ) )
    {
        for (size_t i = 0; i < result.num_rows(); i++)
        {
            
            stockDataList->push_back(new types::StockData( 
                    (std::string)result[i]["Date"],
                    (double)result[i]["open"],
                    (double)result[i]["high"],
                    (double)result[i]["low"],
                    (double)result[i]["close"],
                    (long)result[i]["volume"]));
        }
    }
    
    return(stockDataList);
}

DBAccess::~DBAccess()
{
    if(m_connection.connected())
        m_connection.disconnect();
}

}}} // namespace